<template>
	<!-- 物资归集提交 / 详情 -->
	<view class="">
		<!-- 主表信息 -->
		<view class="mian-table">
			<view class="main-table-title">
				<view class="mian-table-name">
					物资信息
				</view>
			</view>
			<view class="mian-table-content">
				<formItem :formConfig='filterMainCF' :formVal='formValueMain' @searchBaseP='searchBaseP'></formItem>
			</view>
		</view>
		<!-- 子表信息 -->
		<view class="sub-table">
			<view class="sub-table-title">
				<view class="sub-table-title-name">设备信息 </view>
				<view class="sub-table-title-contrl">
					<text class="dicon diconjia-xianxingfangkuang" @click="addMater"></text>
					<text class="dicon diconsaomiao" @click="ScanEvent"></text>
				</view>
			</view>

			<view class="sub-table-content" v-for="(item,index) in formValueSub " :key="index"
				@click="activeSub(index)">
				<formItem :formConfig="filterSubCF" :formVal="item" :formIndex="index" @searchBaseP='searchBaseP'>
				</formItem>
			</view>
		</view>

		<view class="operation">
			<view class="operation-button border-right" @click="finishSubmit">
				提交
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import config from '@/common/config.js'
	import formItem from '@/components/form/form_item.vue'
	import api from '@/common/api.js'
	const plug = uni.requireNativePlugin('Html5app-Gprinter');
	let _self, valueSub, activeType, activeID, activeSubIndex, ResultData = {};
	let t = {
		ware: 1,
		org: 3,
		company: 2
	};
	export default {
		data() {
			return {
				config: [],
				bstypy: "",
				mainType: '',
				subType: '',
				formValueMain: '',
				formValueSub: []
			}
		},
		computed: {
			...mapState(['userInfo', 'checkData']),
			filterMainCF() {
				var t = this.config.App[this.mainType].filter(item => {
					return item.IsShow == 1;
				})
				return t;
			},
			filterSubCF() {
				return this.config.App[this.subType].filter(item => {
					return item.IsShow == 1;
				})
			}
		},
		components: {
			formItem
		},
		onLoad(p) {
			_self = this
			// this.setCheckData([])
			this.config = config
			this.initConifg(p.type)
			this.listenOnce()
			this.mainType = 'repayCFMan'
			this.subType = 'repayCFSub'
			this.bstypy = t[p.type]
			this.$utils.setPageTitle(p.title)
		},
		// onShow() {
		// 	if(this.checkData.length>0){
		// 		this.formValueSub = this.checkData 
		// 	}
		// },
		methods: {
			...mapMutations(["setCheckData"]),
			// 初始化配置数据
			initConifg(bsType) {
				let aa = 'repayCFMan',
					bb = 'repayCFSub';
				let valueMain = this.initForm(this.config.App[aa]),
					valueSub = this.initForm(this.config.App[bb]);
				// valueMain.userId = _self.userInfo.UserId;
				// valueMain.account = _self.userInfo.Account;
				// valueMain.PK_INSTATION = _self.userInfo.UserId;
				// valueMain.MOVER = _self.userInfo.REALNAME;
				// valueMain.PK_MOVER = _self.userInfo.UserId;
				// valueMain.GROUPNAME = _self.userInfo.GROUPNAME;
				// valueMain.PK_GROUP = _self.userInfo.PK_GROUP;
				// valueMain.ORGNAME = _self.userInfo.ORGNAME;
				// valueMain.PK_ORG = _self.userInfo.ORGANIZEID;
				this.formValueMain = this.checkData[0]
				let t = JSON.parse(JSON.stringify(valueSub));
				this.formValueSub.push(t)
				// console.log(_self.userInfo);
			},
			// 激活的第几个子表
			activeSub(i) {
				activeSubIndex = i;
			},

			initForm(config) {
				let valueMain = {};
				config.forEach(item => {
					valueMain[item.ID] = valueMain[item.Default] || ''
				})
				return valueMain;
			},

			// 基础数据查询
			searchBaseP(tnt) {
				activeType = tnt.type
				activeID = tnt.id
				activeSubIndex = tnt.subIndex
				let queryParames =  { PK_MATERIAL : this.formValueMain.PK_MATERIAL } ,
					search_PK_GROUP = '';
				queryParames =	JSON.stringify(queryParames)	
				
				let URL = "/pages/public/search?t=" + activeType + "&queryjson=" + queryParames + '&PK_GROUP=' +
					search_PK_GROUP
				uni.navigateTo({
					url: URL
				});

			},


			// 初始监听
			listenOnce() {
				uni.$on("getSelectItem", function(data) {
					// console.log(888, activeSubIndex);
					switch (activeType) {
						case "EQBOM": // 设备
							_self.formValueSub[activeSubIndex].EQUIPNAME = data.VNAME;
							_self.formValueSub[activeSubIndex].PK_EQUIP = data.BILLNO;
							break;
						default:
							break;
					}
				});
			},

			// 添加物资
			addMater() {
				let AddItem = {
					PK_EQUIP:'',
					EQUIPNAME:'',
					IMPUNUM:''
				}
				this.formValueSub.push(AddItem)
			},

			// 扫描设备
			ScanEvent() {
				
				this.$utils.openScanUtils( 0,0, (res)=>{
					if(res.includes('TYPE:8')){
						let arr = res.split('|');
						let AddItem = {
							PK_EQUIP:'',
							EQUIPNAME:'',
							IMPUNUM:''
						}
						let hasSB = this.formValueSub.filter(i=>{
							return i.PK_EQUIP == arr[2]
						})
						if(!hasSB.length){
							AddItem.EQUIPNAME = arr[1]
							AddItem.PK_EQUIP = arr[2]
							if(!_self.formValueSub[_self.formValueSub.length-1].PK_EQUIP){
								_self.formValueSub[_self.formValueSub.length-1].EQUIPNAME = arr[1]
								_self.formValueSub[_self.formValueSub.length-1].PK_EQUIP = arr[2]
							}else{
								_self.formValueSub.push(AddItem)
							}
						}
					}else{
						this.$utils.tips('非设备二维码,无法使用')
					}
				})
			},

			finishSubmit() {
				let newSub = this.formValueSub.map(item=>{
					let newItem = { ..._self.formValueMain, ...item, IMPUATIONOR:_self.userInfo.Account }
					return newItem 
				})
				let hasSB = this.formValueSub.filter(i=>{
					return i.PK_EQUIP
				})
				if(!hasSB.length){
					return this.$utils.tips('请至少选择一个设备进行归集')
				}
				for (var i = 0; i < _self.formValueSub.length; i++) {
					let item = _self.formValueSub[i];
					if (!item.IMPUNUM && item.PK_EQUIP ) {
						let myIndex = i + 1;
						this.$utils.tips('第' + myIndex + '个设备未输入归集数量');
						return;
					} 
				}
				let savedata = {
					"DETAILS": JSON.stringify(newSub),
				}
				let URL = '/API/APPCommon/SaveImpuation'
				// console.log(URL);
				this.axios({
					url: URL,
					data: savedata,
					isLoading: 1
				}).then(res => {
					_self.$utils.tips('提交成功')
					setTimeout(a => {
						uni.navigateBack({})
					}, 1500)
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 110rpx;
	}

	.mian-table {
		background-color: #ffffff;
		color: #555555;

		.main-table-title {
			margin-top: 20rpx;
			padding: 30rpx 24rpx;
			border-bottom: 1rpx solid #f1f1f1;

			.mian-table-name {
				font-weight: 800;
				font-size: 32rpx;
				position: relative;
				padding-left: 40rpx;
			}

			.mian-table-name:before {
				content: "";
				width: 10rpx;
				height: 32rpx;
				line-height: 32rpx;
				position: absolute;
				margin: 0 16rpx;
				border-radius: 10rpx;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background-color: #1677ff;
			}
		}

		.mian-table-content {
			padding: 0 30rpx;
		}
	}

	.sub-table {
		color: #555555;

		.sub-table-title {
			background-color: #ffffff;
			display: flex;
			margin-top: 20rpx;
			padding: 30rpx 24rpx;
			border-bottom: 1rpx solid #f1f1f1;
			justify-content: space-between;
			align-items: center;

			.sub-table-title-name {
				font-weight: 800;
				font-size: 32rpx;
				position: relative;
				padding-left: 40rpx;
			}

			.sub-table-title-name:before {
				content: "";
				width: 10rpx;
				height: 32rpx;
				line-height: 32rpx;
				position: absolute;
				margin: 0 16rpx;
				border-radius: 10rpx;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background-color: #1677ff;
			}

			.dicon {
				font-size: 50rpx;
				margin: 0 10rpx;
			}
		}

		.sub-table-content {
			margin-top: 20rpx;
			background-color: #ffffff;
			padding: 0 30rpx;
		}
	}
</style>